package com.abstralabs.time;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
	
	public long inittime;
	int runningbutton = -1;
	
	private TimeDatabaseHandler timedatabasehandler;
	private Handler mHandler;
	
	
	List<Button> buttons = new ArrayList<Button>();
	List<TextView> textviews = new ArrayList<TextView>();
	List<History> histories= new ArrayList<History>();
	
	String[] names = {"NMR","English"};
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        // Next line is very useful. It fills acivity with content or context or something. Lots of additional initialization should be done afterwards.
		setContentView(R.layout.activity_main);
        LinearLayout buttonlayout = (LinearLayout)findViewById(R.id.buttonlayout);
        LinearLayout textviewlayout = (LinearLayout)findViewById(R.id.textviewlayout);
    	LinearLayout.LayoutParams layoutparams = new LinearLayout.LayoutParams(
    	            LinearLayout.LayoutParams.MATCH_PARENT,
    	            LinearLayout.LayoutParams.WRAP_CONTENT);

    	timedatabasehandler = new TimeDatabaseHandler(this);
    	timedatabasehandler.open();
    	
    	for (int i=0; i<names.length; i++){  	
        	Button temporarybutton = new Button(this);
        	
        	temporarybutton.setText(names[i]);
        	temporarybutton.setId(i);
        	temporarybutton.setOnClickListener(new View.OnClickListener() {
        		 public void onClick (View view){
        			 buttonClick(view);
        	    }
            });       	
        	buttonlayout.addView(temporarybutton, layoutparams); 
        	
        	TextView temporarytext = new TextView(this);
        	temporarytext.setId(1000+i);
        	temporarytext.setText(names[i]);
        	textviewlayout.addView(temporarytext, layoutparams); 
        	
        	textviews.add(temporarytext);
        	buttons.add(temporarybutton);
        	histories.add(new History(names[i]));
        	}
       // Date date = new Date();
       // inittime = date.getTime();
       // calinit = Calendar.getInstance();
    	
        mHandler = new Handler();
        mHandler.postDelayed(mUpdateGeneration, 1000);
    }

    private Runnable mUpdateGeneration = new Runnable(){
        public void run(){
            Show();
            mHandler.postDelayed(mUpdateGeneration, 100);
        }
    };   
        
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    public void buttonShow (View view){
    	Show();
    }
    
    public void Show(){

    	int h,m,s;
    	for (int i=0;i<buttons.size();i++){
    		histories.get(i).updateSumDay();
    		s=histories.get(i).sumDay;
    		h=s/3600;
    		m=(s % 3600)/60;
    		s= s % 60;
    		textviews.get(i).setText(h+":"+m+":"+s);
    	}
    	
    	//Intent intent=new Intent();
    	//intent.setClass(this, ShowActivity.class);
    	//intent.putExtra(History.class.getCanonicalName(), history);
    	//startActivity(intent);
    	//finish();
    }
    
    public void buttonNewDay (View view){
//    	for (int i=0;i<5;i++)
//    		histories.get(i).newDay();
    }    
    
    public void buttonRemember (View view){
    	for (int i=0;i<histories.size();i++){
    		histories.get(i).sumDay = timedatabasehandler.readEvent(histories.get(i).activity);
    		}
    }  
    
    public void buttonClick (View view){
    	int id = view.getId();
//    	if (runningbutton != id){
//    		for (int i=0;i<histories.size();i++){
//    		histories.get(i).loop=0;
//    		}
//    		System.out.println(id);
//    		histories.get(0).loop=1;
    		runningbutton = id;
    		Calendar calnow=Calendar.getInstance();
    		timedatabasehandler.addEvent(calnow,histories.get(id).activity);
    	   	histories.get(view.getId()).saveHistory(calnow);
//    	}
    }
    
}
